 package com.zxh;

 import android.content.Context;
 import android.content.SharedPreferences;
 import android.os.Build;

 import androidx.annotation.Nullable;

 import com.facebook.react.bridge.ReactApplicationContext;
 import com.zxh.NativeLocalStorageSpec;

 public class NativeLocalStorageModule extends NativeLocalStorageSpec {
     public static final String NAME = "NativeLocalStorage";

     public NativeLocalStorageModule(ReactApplicationContext reactContext) {
         super(reactContext);
     }

     @Override
     public String getName() {
         return NAME;
     }

     @Override
     public void setItem(String value, String key) {
         SharedPreferences sharedPref = getReactApplicationContext().getSharedPreferences("my_prefs",
                 Context.MODE_PRIVATE);
         SharedPreferences.Editor editor = sharedPref.edit();
         editor.putString(key, value);
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
             editor.apply();
         }
     }

     @Override
     public String getItem(String key) {
         SharedPreferences sharedPref = getReactApplicationContext().getSharedPreferences("my_prefs",
                 Context.MODE_PRIVATE);
         String username = sharedPref.getString(key, null);
         return username;
     }

     @Override
     public void removeItem(String key) {
         SharedPreferences sharedPref = getReactApplicationContext().getSharedPreferences("my_prefs",
                 Context.MODE_PRIVATE);
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
             sharedPref.edit().remove(key).apply();
         }
     }

     @Override
     public void clear() {
        SharedPreferences sharedPref = getReactApplicationContext().getSharedPreferences("my_prefs",
        Context.MODE_PRIVATE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
            sharedPref.edit().clear().apply();
        }
     }
 }
